package FileMangerClient;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import Controllers.Context;
import Controllers.RootController;
import DiskManager.Disk;
import FileManager.SimulationDirectory;
import FileManager.SimulationFile;
import Tools.AddSomething;
public class CreateFileController extends RootController{
	Disk disk = new Disk();
     String type  = null;
     Boolean selet_E=false,selet_T=false,selet_F=false,selet_D=false;
     AddSomething addSomething = new AddSomething();
     public Stage stage = new Stage();
    @FXML
    private CheckBox type_T;
    @FXML
    private CheckBox ChoseFile;

    @FXML
    private CheckBox ChoseDDD;
    @FXML
    private Button CreateFile;
    
    @FXML
    private TextField NameField;
    
    private  FlowPane FileMenuPane;
    @FXML
    private CheckBox type_E;

    @FXML
    private TextField SizeFiled;
    public CreateFileController() {
    	 Context.controllers.put(this.getClass().getSimpleName(), this);
    }
    public  CreateFileController(FlowPane FileMenuPane) {
		this.FileMenuPane = FileMenuPane;
	}
    
    @FXML
    void ChoseE(ActionEvent event) {
        if(selet_E!=true)
        {
        	selet_E=true;
        	type_E.setSelected(true);
        	type ="E";
        	if(selet_T==true)
        	{
        		selet_T = false;
        		type_T.setSelected(false);
        	}
        }
        System.out.println(type);
    }
    
    @FXML
    void ChoseT(ActionEvent event) {
    	if(selet_T!=true)
        {
        	selet_T=true;
        	type_T.setSelected(true);
        	type ="T";
        	if(selet_E==true)
        	{
        		selet_E = false;
        		type_E.setSelected(false);
        	}
        }
    	System.out.println(type);
    }

    
    @FXML
    void ChoseFile(ActionEvent event) {
    	if(selet_F!=true)
        {
        	selet_F=true;
        	ChoseFile.setSelected(true);
        	if(selet_D==true)
        	{
        		selet_D = false;
        		ChoseDDD.setSelected(false);
        	}
        }
       
    }

    @FXML
    void ChoseDDD(ActionEvent event) {
    	if(selet_D!=true)
        {
        	selet_D=true;
        	ChoseDDD.setSelected(true);
        	if(selet_F==true)
        	{
        		selet_F = false;
        		ChoseFile.setSelected(false);
        	}
        }
    }
    @FXML
    void Create(ActionEvent event) { 
    	///*
   
    	MyComputerController myComputerController = (MyComputerController)Context.controllers.get(MyComputerController.class.getSimpleName());
    	
    	
    	if(myComputerController.Current_folder.getName().equals("MyComputer"))//如果是在起始面板加入
		   {
			  myComputerController.result.setText("无法在起始目录创建文件或文件夹，请关闭创建窗口");
		   } 
    	else
    	{
    	if(!NameField.getText().equals("")) 
    	{
    	    	
    	   
    		if(selet_D==true)   //文件夹
    		{
    			 SimulationDirectory testD = new SimulationDirectory(NameField.getText(),"D","D");
    			 if(!myComputerController.Current_folder.getChildren().contains(testD)) {
    			   SimulationDirectory newD = new SimulationDirectory(NameField.getText(),"D","D",this.creatNewFile(myComputerController.Current_folder, disk),myComputerController.Current_folder,myComputerController.disk);
    			   newD.setTreeItem(new TreeItem<>(newD.getName()));
    			   if(myComputerController.Current_folder.getStartnum()!=1)myComputerController.Current_folder.saveContentArrary(disk);
    			   newD.saveContentArrary(disk);
    			   
       		       addSomething.CreateVBox("/FileMangerClient/ddd.png",newD,myComputerController);
    			   addSomething.AddTreeItem(newD,myComputerController.Current_folder,myComputerController.getFlowPane());
    		       myComputerController.simulationDirectorys.add(newD);
    		       }
    			 else
    			 {
    				 myComputerController.result.setText("文件夹中已经存在同名的同类型文件");
    			 }
    		} 		
    		else
    		{ 
    			if(selet_T)  //文本文件
    			{
    				SimulationFile testF = new SimulationFile(NameField.getText(),"T","T"); 
    				if(!myComputerController.Current_folder.getChildren().contains(testF)) {
    			 SimulationFile newF = new SimulationFile(NameField.getText(),"T","T",this.creatNewFile(myComputerController.Current_folder, disk),myComputerController.Current_folder,myComputerController.disk); 
    			 if(myComputerController.Current_folder.getStartnum()!=1)myComputerController.Current_folder.saveContentArrary(disk);
    			 newF.saveContentArrary(disk);
    			 addSomething.CreateVBox("/FileMangerClient/File.png",newF,myComputerController);	
    				}
    				 else
        			 {
        				 myComputerController.result.setText("文件夹中已经存在同名的同类型文件");
        			 }
    			}
    			else   //可执行文件
    			{
    				SimulationFile testF = new SimulationFile(NameField.getText(),"E","E");
    				if(!myComputerController.Current_folder.getChildren().contains(testF)) {
    				SimulationFile newF = new SimulationFile(NameField.getText(),"E","E",this.creatNewFile(myComputerController.Current_folder, disk),myComputerController.Current_folder,myComputerController.disk);
    				if(myComputerController.Current_folder.getStartnum()!=1)myComputerController.Current_folder.saveContentArrary(disk);
    				newF.saveContentArrary(disk);
    				addSomething.CreateVBox("/FileMangerClient/File.png",newF,myComputerController);	 
    				}
   				 else
       			 {
       				 myComputerController.result.setText("文件夹中已经存在同名的同类型文件");
       			 }
    			}	
    		}
    		this.stage.close();
    	}
    	else
    		myComputerController.result.setText("文件名不能为空");
    	}
    	
   // */
    	
    }
    
    public int creatNewFile(SimulationDirectory dir,Disk disk) {
		int fatherStartNum = dir.getStartnum();
		char directory = judgeDirectory(dir,fatherStartNum);
		int startnum = creatStartNum(directory,disk);
		if(startnum==-1) {
			System.out.println("not enough space");
		}
		return startnum;
	}
	
	char judgeDirectory(SimulationDirectory dir,int startnum) {
		if(startnum==2) {
			switch(dir.getName()) {
			case("C"):return 'C';
			case("D"):return 'D';
			case("E"):return 'E';
			case("F"):return 'F';
			}
		}
		if(startnum>=3&&startnum<=62)return 'C';
		if(startnum>=63&&startnum<=122)return 'D';
		if(startnum>=123&&startnum<=182)return 'E';
		if(startnum>=183&&startnum<=242)return 'F';
		System.out.println("judgeDirectory_ERROR");
		return 'N';
	}
	
	private int creatStartNum(char directory,Disk disk) {
		int start = 4; 
		int end = 62;
		switch(directory){
		case('C'):break;
		case('D'):start += 60;end += 60;break;
		case('E'):start +=120;end += 120;break;
		case('F'):start +=180;end += 180;break;
		case('N'):return 243;
		}
		int[] fat = disk.getFAT();
		for (int i=start;i<=end;i++) {
			if(fat[i]==0) {
				fat[i]=-1;
				return i;
			}
		}
		return -1;
	}
    
    public final void setDisk(Disk disk) {
		this.disk = disk;
	}
    public final Stage getStage() {
		return stage;
	}
	public final void setStage(Stage stage) {
		this.stage = stage;
	}
    
}
